package zabbix

import (
	"encoding/json"
	"errors"
)

func ItemCreateData() (string,map[string]interface{}) {
	method := "item.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "Free disk space on $1",
        "key_": "vfs.fs.size[/home/joe/,free]",
        "hostid": "30074",
        "type": 0,
        "value_type": 3,
        "interfaceid": "30084",
        "applications": [
            "609",
            "610"
        ],
        "delay": "30s"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ItemDeleteData() (string,map[string]interface{}) {
	method := "item.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "22982",
        "22986"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ItemGetData() (string,map[string]interface{}) {
	method := "item.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "hostids": "10263",
        "search": {
            "key_": "system"
        },
        "sortfield": "name"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ItemUpdateData() (string,map[string]interface{}) {
	method := "item.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "itemid": "10092",
        "status": 0
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}



func ItemCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ItemCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ItemDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ItemDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ItemGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ItemGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ItemUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ItemUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


